/*
 * =====================================================================================
 *
 *       Filename:  atoi.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  01/09/2012 02:30:34 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  gnuhpc (http://blog.csdn.net/gnuhpc), warmbupt@gmail.com
 *        Company:  CMBC
 *
 * =====================================================================================
 */
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <limits.h>

int atoi(char* str)
{
	assert(str);

	int num = 0;
	char *p = str;

	while(*p!='\0' && *p!='.'){
		if(isdigit(*p)){
			num = num*10 + *p - '0';
		}
		
		if( num>INT_MAX)
		{
			assert("Max exceeded!");
		}

		p++;
	}

	p = str;
	if(*p=='-'){
		return -num;	
	}
	else{
		return num;
	}
}

int main(int argc, char *argv[])
{
	printf("%d\n",atoi("133"));
	printf("%d\n",atoi("133.21"));
	printf("%d\n",atoi("-133.21"));
}
